package org.aphelie.torg.ruler.internal.core.text.character;

import java.io.PrintWriter;

import org.aphelie.torg.ruler.core.IModel;
import org.aphelie.torg.ruler.core.character.ICharacterExtension;
import org.aphelie.torg.ruler.internal.core.TGRCore;
import org.aphelie.torg.ruler.internal.core.ischema.ISchema;
import org.aphelie.torg.ruler.internal.core.text.IDocumentAttributeNode;
import org.aphelie.torg.ruler.internal.core.text.IDocumentElementNode;
import org.eclipse.core.runtime.CoreException;


public class CharacterExtensionNode extends CharacterParentNode implements ICharacterExtension {

    private static final long serialVersionUID = 1L;
    private transient ISchema fSchema;

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterExtension#getPoint()
     */
    public String getPoint() {
        return getXMLAttributeValue(P_POINT);
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterExtension#setPoint(java.lang.String)
     */
    public void setPoint(String point) throws CoreException {
        setXMLAttribute(P_POINT, point);
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterObject#setName(java.lang.String)
     */
    public void setName(String name) throws CoreException {
        setXMLAttribute(K_NAME, name);
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterObject#getName()
     */
    public String getName() {
        return getXMLAttributeValue(K_NAME);
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterObject#getTranslatedName()
     */
    public String getTranslatedName() {
        String name = getName();
        if (name != null && name.trim().length() > 0)
            return getResourceString(name);
        String point = getPoint();
        ISchema schema = null; //TODO TGRCore.getDefault().getSchemaRegistry().getSchema(point);
        return schema == null ? "" : schema.getName(); //$NON-NLS-1$
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.IIdentifiable#getId()
     */
    public String getId() {
        return getXMLAttributeValue(K_ID);
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.IIdentifiable#setId(java.lang.String)
     */
    public void setId(String id) throws CoreException {
        setXMLAttribute(K_ID, id);
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.internal.ui.model.plugin.CharacterObjectNode#write()
     */
    public String write(boolean indent) {
        String sep = getLineDelimiter();
        StringBuffer buffer = new StringBuffer();
        if (indent)
            buffer.append(getIndent());
        buffer.append(writeShallow(false));
        IDocumentElementNode[] children = getChildNodes();
        for (int i = 0; i < children.length; i++) {
            children[i].setLineIndent(getLineIndent() + 3);
            buffer.append(sep + children[i].write(true));
        }
        buffer.append(sep + getIndent() + "</extension>"); //$NON-NLS-1$
        return buffer.toString();
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.internal.core.text.plugin.CharacterObjectNode#write(java.lang.String, java.io.PrintWriter)
     */
    public void write(String indent, PrintWriter writer) {
        // Used for text transfers for copy, cut, paste operations
        writer.write(write(true));
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.internal.ui.model.plugin.CharacterObjectNode#writeShallow(boolean)
     */
    public String writeShallow(boolean terminate) {
        String sep = getLineDelimiter();
        String attrIndent = "      "; //$NON-NLS-1$
        StringBuffer buffer = new StringBuffer("<extension"); //$NON-NLS-1$
        IDocumentAttributeNode attr = getDocumentAttribute(K_ID);
        if (attr != null && attr.getAttributeValue().trim().length() > 0)
            buffer.append(sep + getIndent() + attrIndent + attr.write());
        attr = getDocumentAttribute(K_NAME);
        if (attr != null && attr.getAttributeValue().trim().length() > 0)
            buffer.append(sep + getIndent() + attrIndent + attr.write());
        attr = getDocumentAttribute(P_POINT);
        if (attr != null && attr.getAttributeValue().trim().length() > 0)
            buffer.append(sep + getIndent() + attrIndent + attr.write());
        if (terminate)
            buffer.append("/"); //$NON-NLS-1$
        buffer.append(">"); //$NON-NLS-1$
        return buffer.toString();
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.core.plugin.ICharacterExtension#getSchema()
     */
    public Object getSchema() {
        if (fSchema == null) {
            //TODO
            //SchemaRegistry registry = TGRCore.getDefault().getSchemaRegistry();
            //fSchema = registry.getSchema(getPoint());
        } else if (fSchema.isDisposed()) {
            fSchema = null;
        }
        return fSchema;
    }

    /* (non-Javadoc)
     * @see org.eclipse.pde.internal.core.text.plugin.CharacterObjectNode#reconnect(org.eclipse.pde.core.plugin.ISharedCharacterModel, org.eclipse.pde.internal.core.ischema.ISchema, org.eclipse.pde.internal.core.text.IDocumentElementNode)
     */
    public void reconnect(IDocumentElementNode parent, IModel model) {
        super.reconnect(parent, model);
        // Transient Field:  Schema
        // Not necessary to reconnect schema.
        // getSchema will retrieve the schema on demand if it is null       
        fSchema = null;
    }

}
